<?php
namespace app\admin\controller;

use think\Loader;
use org\Rbac;
class Index extends Auth
{
    public function __construct()
    {
        parent::__construct();
    }
	
    public function index()
    {
		//dump(VENDOR_PATH);
		$this->isLogin();
		
		//获取各自的访问次数
		$info = M('visitor')->select();
        //初始化
        $one_info = $color = [];
		foreach($info as $key=>$data){
			$one_info[] = $data['type'];
			$color[] = $data['color'];
		}
        $num = array_count_values($one_info);
		$this->view->assign('num',$num);//来源名称
		$this->view->assign('color',$color);//产生随机颜色
		
		//计算时间段
			 $now_time = time();
			 $time = date('H:i:s',$now_time);
			 $minus = date("i",$now_time);
			 $hous =date('H',$now_time);
			
		//循环减去8个小时,得出时间分段
			 $time_fenqu="";
			 for($i=8;$i>=0;$i--){
				 $time_fenqu .='"'.($hous-$i).':59",';
				 $time_info[]= date('Y-m-d').'-'.($hous-$i);
			 }
			 $this->assign('time',$time_fenqu);
			 
			 
			/**
			 *  每个时间段的IP，这里的IP是指不包含十分钟内统一ip地址访问的个数
			 */
			 $total_ip = M('visitor')->field('time,count(time) as timenum')->group('time')->select();
			 
			 $total_time=[];
			 $total_timenum=[];
			 foreach($total_ip as $key=>$value){
				 $total_time[]=$value['time'];
				 $total_timenum[]=$value['timenum'];
			 }
			$new_total_ip =array_combine($total_time,$total_timenum);
			
			//将所有的时间分组
			 $have_time = M('visitor')->field('time')->group('time')->select();
			 $new_have_time=[];
			 foreach($have_time as $key=>$value){
				 $new_have_time[]=$value['time'];
			 }
			 //如果循环出来的时间段在$new_have_time中就找出对应的统计，否则，设置成为0
			 //$value 为循环出来的时间,
			  $new_total="";
             foreach($time_info as $key=>$value){
                  if(in_array($value,$new_have_time)){
					 $total =$new_total_ip[$value];
				  }else{
					 $total =0;
				  }
				  $new_total .=$total.',';
			 }
			$this->assign('num_ip',$new_total);
			 
			
		   /**
		    *  每个时间段的独立访客，统计出每个时间段不重复ip的个数
		    */
			//1.group(time,ip) 两个字段，会自动合并相同的ip,注意顺序，time作为键名，ip作为键值
			$fangke = M('visitor')->field('time,ip')->group('time,ip')->select();
            //2.只要看时间有几个，就可以确定有几个ip,不必知道具体的IP
			$fangke_time=[];
			foreach($fangke as $key=>$value){
              $fangke_time[]=$value['time'];
			}
			//3.利用array_count_values()，进行计数通知，最后传入时间就得出出现的次数
			$fangke_num = array_count_values($fangke_time);
			//4.根据时间段进行循环判断即可
			$du_fangke="";
			foreach($time_info as $key=>$value){
                if(in_array($value,$new_have_time)){
					 $fangke_total =$fangke_num[$value];
				  }else{
					 $fangke_total =0;
				}
				$du_fangke .=$fangke_total.',';
			}
			$this->assign('du_ip',$du_fangke);			

		
		//循环出每个小时的流量信息

		/**
		 * 分成两个数组，time 与 num 分别为键值,再组合
		 */
		$info_pv = M('count')->field('time,sum(num) as zonghe')->group('time')->select();
		$info_time=[];
		$info_num=[];
         foreach($info_pv as $key=>$value){
			 $info_time[]=$value['time'];
			 $info_num[]=$value['zonghe'];
		 }
		$new_pv =array_combine($info_time,$info_num);
		/**
		 *如果一个时间段数据库中没有，就会报错，判断数据库中是否有这个时间
		 *所有今天的所有时间，判断是否存在。
		 */
		$t_time = M('count')->field('time')->group('time')->select();
        $today_time=[];
		foreach($t_time as $key=>$value){
			$today_time[]=$value['time'];
		}
		$num_pv="";
		foreach($time_info as $key=>$value){
           if(in_array($value,$today_time)){
			  $icount=$new_pv[$value];
		   }else{
			   $icount=0;
		   }
		   
			$num_pv .= $icount.',';
		}
		$this->assign('num_pv',$num_pv);
		//循环出每个小时的流量信息 END
		
		
		
		
		/**
		 *  最近四天的 PV,IP,独立访客的信息
		 */
		 
		 //PV
		$day_pv = M('count')->field('days,sum(num) as daysnum')->group('days')->select();
        $pv_day=[];
		$pv_num=[];
        foreach($day_pv as $key=>$value){
			$pv_day[]=$value['days'];
			$pv_num[]=$value['daysnum'];
		}	
        $new_pv_day = array_combine($pv_day,$pv_num);
   		 for($i=0;$i<=3;$i++){
			 if(!empty($new_pv_day)){
			 $day[$i]= date('n-j',time()-$i*86400);
			 if(array_key_exists($day[$i],$new_pv_day)){
				$this->assign('sum'.$i,$new_pv_day[$day[$i]]);
			 }else{
				$this->assign('sum'.$i,0);
			 }
			 }else{
			 $this->assign('sum'.$i,0); 
			 }
		 }
		 //PV END
			
		//IP,通过计算一天的数据出现的次数，得出IP，此IP在写入时有限制
		$day_ip =M('visitor')->field('date,count(date) as datenum')->group('date')->select();
        $ip_day=[];
		$ip_datenum=[];
		foreach($day_ip as $key=>$value){
			$ip_day[]=$value['date'];
			$ip_datenum[]=$value['datenum'];
		}
		//如果,当前包含当前时间的四天时间中，存在没有写进数据库的，设置成
	   //默认值0
		$new_ip_day = array_combine($ip_day,$ip_datenum);
		for($i=0;$i<=3;$i++){
			if(!empty($new_ip_day)){
			$day[$i]= date('n-j',time()-$i*86400);
				if(array_key_exists($day[$i],$new_ip_day)){
					$this->assign('day_ip'.$i,$new_ip_day[$day[$i]]);
				}else{
					$this->assign('day_ip'.$i,0);
				 }
			}else{
			 $this->assign('day_ip'.$i,0);
			}

		}
        //IP END
		
		//独立访客
		$du_fangke = M('visitor')->field('date,ip')->group('date,ip')->select();
		$du_fang=[];
		foreach($du_fangke as $key=>$value){
              $du_fang[]=$value['date'];
		}
        $du_fangke_num = array_count_values($du_fang);
   		 for($i=0;$i<=3;$i++){
			 if(!empty($new_ip_day)){
			 $day[$i]= date('n-j',time()-$i*86400);
			 if(array_key_exists($day[$i],$du_fangke_num)){
				 $this->assign('dulifangke'.$i,$du_fangke_num[$day[$i]]); 
			 }else{
				 $this->assign('dulifangke'.$i,0); 
			 }
			
			 }else{
			 $this->assign('dulifangke'.$i,0); 
			 }
		 }
		//独立访客 END
		
		
		
		/**
		 *  删除一天数据，保留最新四天，如果有五天的数据 则是删除最早的一天，留下最新四天
		 */
		 $today_time['days'] = date('n-j',time()-4*86400);
		 M('count')->where($today_time)->delete();
		 $del_four['date']=date('n-j',time()-4*86400);
		 M('visitor')->where($del_four)->delete();
			

			return $this->fetch();
    }
	
    public function stater()
    {
        return $this->fetch();
    }
	
	/**
	 *  根据ip获取所在地区
	 */
	
	
	public function ip()
	{
		$ip = get_client_ip();
		$Ip = new \util\IpLocation('UTFWry.dat');
		$area = $Ip->getlocation($ip);
		$country = $area['country'];
		$this->view->assign('area',$country);
		 return $this->view->fetch();
	}

	/**
	 *  分成两个数组，time 与 num 分别为键值,再组合
	 */
	
	
	
	
	
	
	
	
	
}
